﻿#r @"..\Microsoft.SharePoint.Client (spo)\Microsoft.SharePoint.Client.dll"
#r @"..\Microsoft.SharePoint.Client (spo)\Microsoft.SharePoint.Client.Runtime.dll"
#r @"..\Microsoft.SharePoint.Client (spo)\Microsoft.SharePoint.Client.DocumentManagement.dll"
#r @"..\Microsoft.SharePoint.Client (spo)\Microsoft.SharePoint.Client.Taxonomy.dll"

#load "Client.fs"

open Client
open Microsoft.SharePoint.Client
open Microsoft.SharePoint.Client.Application
open Microsoft.SharePoint.Client.Taxonomy
open Microsoft.SharePoint.Client.WebParts
open Microsoft.SharePoint.Client.DocumentManagement
open Microsoft.SharePoint.Client.DocumentSet
open System.Security;
open System;
open System.Xml;
open System.Collections.Generic
    
// ======================================================================================
// Capturing, modify homepage, change ContentType order, modify DocumentSet Template
// ======================================================================================

let web = Client.Context.Web
Client.Load web

Client.Context.Url

let list = Client.Context.Web.Lists.GetByTitle("Documents")

Client.Load list

// Capture ======================================

let item = list.GetItemById(1)

Client.Load item.Folder.Properties

for prop in item.Folder.Properties.FieldValues do
    let msg = prop.Key.ToString() + " | " + prop.Value.ToString()
    printfn "%s\n" msg

let snapshotXML = item.Folder.Properties.FieldValues.["snapshots"].ToString()

// Snapshots look like this:
//
//    @"<SnapshotCollection NextSnapshotNumber=""3"" NextInternalId=""4"">
//	    <Items>
//		    <Item Id=""1"" Guid=""ab91e886-7198-4a99-8d54-73c12b7c7434"" Url=""Configuration - Content Type Hub.docx"" LinkToDoc=""False"" />
//		    <Item Id=""2"" Guid=""b9a0980e-77bd-430e-a74d-12a8a0348fc6"" Url=""Sharing Subfolder.docx"" LinkToDoc=""False"" />
//		    <Item Id=""3"" Guid=""5e2b9820-66e9-4c1b-baba-bcc5f98ba102"" Url=""Tagging Overview.docx"" LinkToDoc=""False"" />
//	    </Items>
//	    <Snapshots>
//		    <Snapshot Label=""2"" Major=""False"" Created=""05/15/2015 09:04:26"" By=""me@geek.onmicrosoft.com"">
//			    <Comments>Test Capture 02</Comments>
//			    <Fields>
//				    <Field Id=""8553196d-ec8d-4564-9861-3dbe931050c8"">test2</Field>
//				    <Field Id=""fa564e0f-0c70-4ab9-b863-0177e6ddd247"">test2</Field>
//				    <Field Id=""cbb92da4-fd46-4c7d-af6c-3128c2a5576e"" />
//				    <Field Id=""64a19d9b-f618-4768-986a-96acd2d120e5"">1;#test|0a760d86-e480-43c6-9024-e15a2fb69c90</Field>
//				    <Field Id=""54fa28aa-e773-4d9c-92cc-e608039e69dd"">1</Field>
//				    <Field Id=""235ea863-bd99-4548-819e-a4aff3858593"">6</Field>
//				    <Field Id=""24543794-aab2-4a64-a672-280526a67dce"">No</Field>
//				    <Field Id=""5548a3b4-4673-4810-ac16-34a804b4e305"">No</Field>
//				    <Field Id=""2a289da3-8095-44ca-a908-53fef8556a5c"">3;#Baseline3|cc189fc7-ce8e-4c14-b704-688f95f09e90</Field>
//			    </Fields>
//			    <SnapshotItems>
//				    <SnapshotItem Id=""1"" Version=""0.1"" />
//				    <SnapshotItem Id=""2"" Version=""0.1"" />
//				    <SnapshotItem Id=""3"" Version=""0.1"" />
//			    </SnapshotItems>
//		    </Snapshot>
//		    <Snapshot Label=""1"" Major=""True"" Created=""05/15/2015 08:48:15"" By=""me@geek.onmicrosoft.com"">
//			    <Comments>Test Capture 1</Comments>
//			    <Fields>
//				    <Field Id=""8553196d-ec8d-4564-9861-3dbe931050c8"">test2</Field>
//				    <Field Id=""fa564e0f-0c70-4ab9-b863-0177e6ddd247"">test2</Field>
//				    <Field Id=""cbb92da4-fd46-4c7d-af6c-3128c2a5576e"" />
//				    <Field Id=""64a19d9b-f618-4768-986a-96acd2d120e5"">1;#test|0a760d86-e480-43c6-9024-e15a2fb69c90</Field>
//				    <Field Id=""54fa28aa-e773-4d9c-92cc-e608039e69dd"">1</Field>
//				    <Field Id=""235ea863-bd99-4548-819e-a4aff3858593"">3</Field>
//				    <Field Id=""24543794-aab2-4a64-a672-280526a67dce"">No</Field>
//				    <Field Id=""5548a3b4-4673-4810-ac16-34a804b4e305"">No</Field>
//				    <Field Id=""2a289da3-8095-44ca-a908-53fef8556a5c"">3;#Baseline3|cc189fc7-ce8e-4c14-b704-688f95f09e90</Field>
//			    </Fields>
//			    <SnapshotItems />
//		    </Snapshot>
//	    </Snapshots>
//    </SnapshotCollection>"
    
let xmlDoc = new XmlDocument();
xmlDoc.LoadXml(snapshotXML);
let nextSnapshotNumber = xmlDoc.DocumentElement.GetAttribute("NextSnapshotNumber") 

let xmlSnapshotList = xmlDoc.GetElementsByTagName("Snapshot")
for xmlElem in xmlSnapshotList do
    let msg = xmlElem.Attributes.["Label"].get_InnerText()
    printfn "%s\n" msg

let xmlItemList = xmlDoc.GetElementsByTagName("Item")
for xmlElem in xmlItemList do
    let url = xmlElem.Attributes.["Url"].get_InnerText()
    let file = item.Folder.Files.GetByUrl(url)
    Client.Load file
    let fItem = file.ListItemAllFields
    Client.Load fItem
    let msg = fItem.Id.ToString() + ": " + url
    printfn "%s\n" msg

// Modify Homepage ======================================

let spFile = Client.Context.Web.GetFileByServerRelativeUrl("/_cts/MyContentType/docsethomepage.aspx")
let wpManager = spFile.GetLimitedWebPartManager(PersonalizationScope.Shared)
let wps = wpManager.WebParts
Client.Load wps

for wp in wps do
    Client.Load wp.WebPart
    let msg = wp.WebPart.Title + " " + wp.Id.ToString()
    printfn "%s\n" msg

let imageWP = wps.GetById(Guid.Parse("81a45d98-9053-4ca9-a719-fadb7f1f3d70"))
Client.Load imageWP.WebPart

let props = imageWP.WebPart.Properties

for fval in props.FieldValues do
    let msg = fval.Key
    printfn "%s\n" msg

//=== Content Type ===

Client.Load list.ContentTypes
Client.Load list.RootFolder
let myList = new List<ContentTypeId>()
for ct in list.ContentTypes do
    Client.Load ct
    if ct.Name <> "Document Set" then
        myList.Add(ct.Id)
        let msg = ct.Name + " | " + ct.StringId + " | " +  ct.Id.StringValue
        printfn "%s\n" msg
    list.RootFolder.UniqueContentTypeOrder <- myList
    Client.ExecuteQuery()

    list.RootFolder.Update()
    list.Update()

let rootfld = list.RootFolder

Client.Load list.RootFolder.Properties


for ct in list.RootFolder.ContentTypeOrder do    
    let msg = ct.StringValue
    printfn "%s\n" msg

for prop in list.RootFolder.Properties.FieldValues do    
    let msg = prop.Key + "|" + prop.Value.ToString()
    printfn "%s\n" msg


//=== DocumentSetTemplate ===


Client.Load Client.Context.Web.ContentTypes
for ct in Client.Context.Web.ContentTypes do
    Client.Load ct
//    if ct.Name = "Baseline" then
    let msg = ct.Name + "|" + ct.Id.ToString()
    printfn "%s\n" msg

let ct = Client.Context.Web.ContentTypes.GetById("0x0120D520006F3F49F24208D846809A8D379500AA6A")
let template = DocumentSetTemplate.GetDocumentSetTemplate(Client.Context, ct);
Client.Context.Load(template);
Client.ExecuteQuery();

Client.Load template.WelcomePageFields
for fld in template.WelcomePageFields do
    Client.Load fld
    let msg = fld.Title
    printfn "%s\n" msg

let tempFld = Client.Context.Web.Fields.GetByInternalNameOrTitle("BaselineTemplate")
Client.Load tempFld
template.WelcomePageFields.Add(tempFld)
template.Update(true)
Client.ExecuteQuery()

let docset = DocumentSet.Create(Client.Context, item.Folder, "Test", ct.Id)

Client.Load template.DefaultDocuments
template.DefaultDocuments.Count
for doc in template.DefaultDocuments do
    Client.Load doc
    let msg = doc.Name
    printfn "%s\n" msg

Client.Load template.AllowedContentTypes
template.AllowedContentTypes.Count

let cta = Client.Context.Web.ContentTypes.GetById("0x0101");
Client.Load cta
template.AllowedContentTypes.Remove(cta.Id)
template.Update(true)
Client.ExecuteQuery()
